package com.wish233.wiliwilisearch.controller;

import com.wish233.wiliwilicommon.common.BaseResponse;
import com.wish233.wiliwilicommon.common.ErrorCode;
import com.wish233.wiliwilicommon.common.ResultUtils;
import com.wish233.wiliwilicommon.exception.BusinessException;
import com.wish233.wiliwilicommon.exception.ThrowUtils;
import com.wish233.wiliwilisearch.domain.result.SearchResponse;
import com.wish233.wiliwilisearch.service.SearchService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author WHH
 */
@RestController
public class SearchController {

    @Resource
    private SearchService searchService;

    @GetMapping("")
    @ApiOperation("搜索")
    public BaseResponse<SearchResponse> searchByKeyWord(@RequestParam Integer searchType,
                                                        @RequestParam String keyWord,
                                                        @RequestParam Integer current,
                                                        @RequestParam Integer pageSize) {
        ThrowUtils.throwIfParamNull(searchType, keyWord);
        ThrowUtils.throwIf(current <= 0 || pageSize <= 0, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(searchService.searchByKeyWord(searchType, keyWord, current, pageSize));
    }
}
